package qingke.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import qingke.command.impl.AccountCommand;
import qingke.command.impl.BuyCommand;
import qingke.command.impl.DepositCommand;
import qingke.command.impl.PayCommand;
import qingke.command.impl.ProductCommand;
import qingke.command.impl.OrderCommand;
import qingke.pojo.User;

public class UserCommandFactory extends SystemCommandFactory {
	private User user;

	UserCommandFactory(User user) {
		this.user = user;
	}

	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

	static {
		commandMap.put(CommandCode.PRODUCT, ProductCommand.class);
		commandMap.put(CommandCode.BUY, BuyCommand.class);
		commandMap.put(CommandCode.PAY, PayCommand.class);
		commandMap.put(CommandCode.ORDER, OrderCommand.class);
		commandMap.put(CommandCode.ACCOUNT, AccountCommand.class);
		commandMap.put(CommandCode.DEPOSIT, DepositCommand.class);
	}

	public Command buildCommand(CommandCode code) {

		Command command = null;

		Class<? extends Command> cmdClass = commandMap.get(code);

		if (cmdClass != null) {
			try {
				command = cmdClass.getConstructor(User.class).newInstance(user);
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}

		if (command == null) {
			command = super.buildCommand(code);
		}

		return command;
	}
}
